from PIL import Image
import os


def f(path, x, y):
    img = Image.open(path)
    rows = img.size[0]-6
    cols = img.size[1]-6
    row_start = int(rows/11*(x-1))+3
    col_start = int(cols/11*(y-1))+3
    row_end = int(rows / 11 * x)+3
    col_end = int(cols / 11 * y)+3
    # 定义左，上，右和下像素坐标
    BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN = row_start, col_start, row_end,  col_end
    # 从此图像返回一个矩形区域。 盒子是一个4元组定义左，上，右和下像素坐标。
    box = (BOX_LEFT, BOX_UP, BOX_RIGHT, BOX_DOWN)
    # 进行ROI裁剪
    roi_area = img.crop(box)
    # 裁剪后每个图像全路径
    image_output_path = folder + IMG
    # 保存处理后的图像
    roi_area.save(image_output_path)

folder = '../资源/图片/'
IMG = '章鱼9.png'
if not os.path.exists(folder+IMG):
    f(folder+'24层.png', 7, 4)   # 按坐标输，不按索引